/********************************
 * ETK - Easy ToolKit
 * Copyright (C) 2007-2008 Ekinoks (scv.ekinoks@gmail.com)
 * All rights reserved.
 *
 *    This file is part of ETK.
 *
 *    Foobar is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    Foobar is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Foobar; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 ***********************************************************************************/

// ceci est un teste

#include <iostream>

#include "etk/define.h"

#include "etk/initialization.h"
#include "etk/Render/Render.h"
#include "etk/Render/ToolKit/ToolKit.h"
#include "etk/Render/ToolKit/LibGtk.h"
#include "etk/Widget/Widget.h"
#include "etk/Widget/WidgetContainer/Window/Window.h"
#include "etk/Widget/WidgetContainer/Box/Box.h"
#include "etk/CallBack/CallBack.h"
#include "etk/Widget/Button/Button.h"
#include "etk/Event/Event.h"
#include "etk/Event/EventButton/EventButton.h"

#include "etk/StateModel/StateModel.h"

#include "etk/Shutter/ShutterScroll/ShutterScroll.h"
#include "etk/Shutter/ShutterScroll/ShutterScrollModel.h"

#include <gtk/gtk.h>


using namespace etk;
using namespace boost;

// TODO : creer un container pour les widget qui répond a la régle : ce qui a été "new" doit étre "delete"

void fonction_test()
{
    printf("CouCou !\n");
}

void fonction_action_button(EventButton eb)
{
    printf("x_root : %f\n",eb.getXRoot());  // position par raport a l'écrant
    printf("y_root : %f\n",eb.getYRoot());  // position par raport a l'écrant

    printf("x : %f\n",eb.getX());   // position par raport a l'objet
    printf("y : %f\n",eb.getY());   // position par raport a l'objet

//    printf("button : %d\n", eb.getButton()); // N° du bouton
    printf("button1 : %d\n", eb.getShutterButton() == ShutterButtonModel::BUTTON1);

    printf("Alt = %d\n", eb.getState() == StateModel::ALT+StateModel::BUTTON3);



    /// Pour tester les events
    Window *window = new Window();

    window->toButtonEvent(  bind(fonction_action_button, _EVENT),
                                eb.getShutterButton());

}

void fonction_action_key(EventKey ek)
{
    cout << "fonction_action_key !" << endl;
    cout << "char : " << ek.getShutterKey().getCharacters() << endl;
    cout << "keycode : " << ek.getShutterKey().getKeyCode() << endl << endl;
}

void fonction_action(Event e)
{

}

/**
POUR TESTER LES FUITE MEMOIRE
**/
gboolean f(gpointer data)
{
    static int test=0;
    static Window *win=NULL;
    static CallBack * call=NULL;
        //usleep(1000);
    static Button * b=NULL;


    if(test==0)
    {
        win = new Window("TEST", 100, 100, 100, 100);
        call = win->toButtonEvent( bind(fonction_test) );
//        new CallBack(win, EVENT_DELETE, bind(fonction_test));

        new Button(win, "test");
        new Button(win, "test");
        b=new Button(win, "test");
        new Button(win, "test", boost::bind(f, b));



    }else if(test==1)
    {
        delete b;
        delete call;
    }else if(test==2)
    {
        win->destroy();
        //delete win;
    }else
    {
     printf("ERREUR !\n");
    }

    test = (test+1)%3;

    return true;
}

void supprimer(Widget *b)
{
    delete b;
}

void new_bouton(Box *box)
{
    new Button(box, "Sucide", bind(supprimer, _ME));
}

int f(PWIDGET widget, int event, void* arg)
{
        printf("test\n");
        return 0;
}

int main (int argc, char *argv[])
{
    /// Inisalisation de la ToolKit avec LibGtk
    init(new LibGtk(), argc, argv);

    /// Création de la fenétre
    Window * window = new Window("Méthode 1", 640, 480, 100, 100);
    window->toDeleted( bind(exit, 0) );

    new Button(window, "bouton");

    /// Ajouter des elements
    Box *box = new Box(window, HORIZONTAL, true, 5);

    new Button(box, "Nouvaux bouton", bind(new_bouton, box));

    Button *b = new Button(window, "Action", bind(fonction_action, _EVENT));//->toButtonPressed(bind(fonction_test2, _EVENT));
    b->toButtonEvent( bind(fonction_action_button, _EVENT) );

    box = new Box(window, HORIZONTAL, true, 5);
    new Button(box, "Fermer fenetre", bind(supprimer, window));
    new Button(box, "Quiter", bind(exit, 1));

    new Button(box, "me rendre invisible", bind(&Button::hide, _ME));


    /// Pour tester les events
    window = new Window();

    window->toButtonEvent(  bind(fonction_action_button, _EVENT),
                                ShutterButtonModel::BUTTON1 || ShutterButtonModel::DOUBLE_CLICK || ShutterButtonModel::TRIPLE_CLICK,
                                StateModel::CTRL + StateModel::SUPER - StateModel::SHIFT);


    window->toScrolled(     bind(fonction_test),
                                ShutterScrollModel::SCROLL_DOWN,
                                StateModel::CTRL);


    window->toKeyPressed(   bind(fonction_action_key, _EVENT),
                                ShutterKeyModel("azeAZE €")/*,
                                StateModel::BUTTON1*/);

    window->toKeyPressed(   bind(fonction_action_key, _EVENT),
                                ShutterKeyModel(28),
                                StateModel::CTRL);


    window->toMotionNotify( bind(fonction_test),
                                StateModel::CTRL);


    /// POUR TESTER LES FUITES MEMOIRES
    //g_timeout_add(10, f, NULL);

    // Depart de la boucle d'évenements !
    go();
}

